package at.dl.android.dota.adviser;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import at.dl.android.dota.adviser.R;

public class ShowShopActivity extends ActivityWithDB {
    
    final public static String SHOP_NAME = "SHOP_NAME";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_shop);

        final Bundle extras = getIntent().getExtras();
        String name = extras.getString(SHOP_NAME);

        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        String qry = "SELECT * FROM shops WHERE name = \"" + name + "\"";
        Cursor cursor = db.rawQuery(qry,new String[0]);
        cursor.moveToFirst();

        setHead(name,cursor);

        TabHost tabHost = (TabHost) findViewById(R.id.tabHostShops);
        tabHost.setup();
        setOverviewMap(cursor,tabHost);
        setSentinelMap(cursor,tabHost);            
        setScourgeMap(cursor,tabHost);  
        cursor.close();
        setItems(name,tabHost);
    }

    public void setOverviewMap ( Cursor cursor, TabHost tabHost ) {
        String overviewMap = cursor.getString(cursor.getColumnIndex(ColumnNames.Shops.overview_map));
        TabSpec spec1 = tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.id.overviewMapLL);
        spec1.setIndicator("Overview Map",getResources().getDrawable(R.drawable.tab_overview_map));
        prepareWebView(R.id.overviewWebView,overviewMap);
        tabHost.addTab(spec1);
    }

    public void setSentinelMap ( Cursor cursor, TabHost tabHost ) {
        String detailedSentinelMap = cursor.getString(cursor.getColumnIndex(ColumnNames.Shops.detailed_map_sentinel));
        TabSpec spec2 = tabHost.newTabSpec("Tab 2");
        spec2.setContent(R.id.sentinelMapLL);
        spec2.setIndicator("Map Sentinel",getResources().getDrawable(R.drawable.tab_detail_map_sentinel));
        tabHost.addTab(spec2);
        if ( detailedSentinelMap != null && detailedSentinelMap.length() > 0 ) 
            prepareWebView(R.id.detailedSentinelWebView,detailedSentinelMap);
        else
            tabHost.getTabWidget().getChildAt(1).setVisibility(View.GONE);
    }

    public void setScourgeMap ( Cursor cursor, TabHost tabHost ) {
        String detailedScourgeMap = cursor.getString(cursor.getColumnIndex(ColumnNames.Shops.detailed_map_scourge));
        TabSpec spec3 = tabHost.newTabSpec("Tab 3");
        spec3.setContent(R.id.scourgeMapLL);
        spec3.setIndicator("Map Scourge",getResources().getDrawable(R.drawable.tab_detail_map_scourge));
        tabHost.addTab(spec3);
        if ( detailedScourgeMap != null && detailedScourgeMap.length() > 0 )
            prepareWebView(R.id.detailedScourgeWebView,detailedScourgeMap);
        else
            tabHost.getTabWidget().getChildAt(2).setVisibility(View.GONE);
    }

    public void setItems ( String shopName, TabHost tabHost ) {
        SQLiteDatabase db = getDataBaseHelper().getReadableDatabase();
        TabSpec spec4 = tabHost.newTabSpec("Tab 4");
        spec4.setContent(R.id.shopItemsLL);
        spec4.setIndicator("Items",getResources().getDrawable(R.drawable.tab_broadsword));
        tabHost.addTab(spec4);
        String whereItems = " WHERE " + ColumnNames.Items.shop1 + " = \"" + shopName + "\"" 
                          + " OR "    + ColumnNames.Items.shop2 + " = \"" + shopName + "\""
                          + " OR "    + ColumnNames.Items.shop3 + " = \"" + shopName + "\"";
        final String query = "SELECT " + ColumnNames.Items.name + ", " + ColumnNames.Items.item_image_id + " FROM ITEMS" + whereItems;
        Cursor itemCursor = db.rawQuery(query,new String[0]);
        int cnt = itemCursor.getCount();
        boolean hasData = itemCursor.moveToFirst();
        String[] itemNames = new String[cnt];  
        int[] itemIDs = new int[cnt];
        for ( int i = 0; hasData && i < cnt; i++ ) {
            itemNames[i] = itemCursor.getString(itemCursor.getColumnIndex(ColumnNames.Items.name));
            itemIDs[i] = itemCursor.getInt(itemCursor.getColumnIndex(ColumnNames.Items.item_image_id));
            hasData = itemCursor.moveToNext();
        }
        itemCursor.close();
        GridView itemGridView = (GridView) findViewById(R.id.items);
        CallActivityImageAdapter sia = new CallActivityImageAdapter(this,ShowItemActivity.class,ShowItemActivity.ITEM_NAME,itemIDs,itemNames);
        itemGridView.setAdapter(sia);
    }

    private void prepareWebView ( int webViewId, String overviewMap ) {
        WebView webView = (WebView) findViewById(webViewId);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setBackgroundColor(Color.parseColor("#000000"));
        webView.loadUrl("file:///android_asset/" + overviewMap); 
    }

    private void setHead ( String name, Cursor cursor ) {
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageResource(cursor.getInt(cursor.getColumnIndex(ColumnNames.Shops.shop_image_id)));
        TextView t = (TextView) findViewById(R.id.name);
        t.setText(name);
    }
}
